﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using FZKJ.DBUtility;
namespace FZKJ.DAL
{
    /// <summary>
    /// 数据访问类:buss_vehicle
    /// </summary>
    public partial class buss_vehicle
    {

        private readonly SQLHelper sqldal = null;
        public buss_vehicle(string connStr = "")
        {
            sqldal = new SQLHelper(connStr);
        }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from buss_vehicle");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.VarChar,36)           };
            parameters[0].Value = id;

            return sqldal.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(FZKJ.Model.buss_vehicle model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into buss_vehicle(");
            strSql.Append("id,vehiclecode,carowner,carmodel,licenseplate,drivingnumber,enginenumber,pedestal,mobile,transportcompany,nuclearweight,initialdate,businessattribution,businessdepartment,businessowner,option1,option2,option3,inputtime,isdel,deltime,inputor,updatetime,updateor,state,activityState)");
            strSql.Append(" values (");
            strSql.Append("@id,@vehiclecode,@carowner,@carmodel,@licenseplate,@drivingnumber,@enginenumber,@pedestal,@mobile,@transportcompany,@nuclearweight,@initialdate,@businessattribution,@businessdepartment,@businessowner,@option1,@option2,@option3,@inputtime,@isdel,@deltime,@inputor,@updatetime,@updateor,@state,@activityState)");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.VarChar,36),
                    new SqlParameter("@vehiclecode", SqlDbType.VarChar,30),
                    new SqlParameter("@carowner", SqlDbType.VarChar,36),
                    new SqlParameter("@carmodel", SqlDbType.VarChar,100),
                    new SqlParameter("@licenseplate", SqlDbType.VarChar,100),
                    new SqlParameter("@drivingnumber", SqlDbType.VarChar,100),
                    new SqlParameter("@enginenumber", SqlDbType.VarChar,100),
                    new SqlParameter("@pedestal", SqlDbType.Int,4),
                    new SqlParameter("@mobile", SqlDbType.VarChar,11),
                    new SqlParameter("@transportcompany", SqlDbType.VarChar,200),
                    new SqlParameter("@nuclearweight", SqlDbType.NVarChar,50),
                    new SqlParameter("@initialdate", SqlDbType.DateTime),
                    new SqlParameter("@businessattribution", SqlDbType.VarChar,10),
                    new SqlParameter("@businessdepartment", SqlDbType.VarChar,100),
                    new SqlParameter("@businessowner", SqlDbType.VarChar,100),
                    new SqlParameter("@option1", SqlDbType.VarChar,100),
                    new SqlParameter("@option2", SqlDbType.VarChar,100),
                    new SqlParameter("@option3", SqlDbType.VarChar,100),
                    new SqlParameter("@inputtime", SqlDbType.DateTime),
                    new SqlParameter("@isdel", SqlDbType.Int,4),
                    new SqlParameter("@deltime", SqlDbType.DateTime),
                    new SqlParameter("@inputor", SqlDbType.NVarChar,50),
                    new SqlParameter("@updatetime", SqlDbType.DateTime),
                    new SqlParameter("@updateor", SqlDbType.NVarChar,50),
                    new SqlParameter("@state", SqlDbType.Int,4),
                    new SqlParameter("@activityState", SqlDbType.Int,4)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.vehiclecode;
            parameters[2].Value = model.carowner;
            parameters[3].Value = model.carmodel;
            parameters[4].Value = model.licenseplate;
            parameters[5].Value = model.drivingnumber;
            parameters[6].Value = model.enginenumber;
            parameters[7].Value = model.pedestal;
            parameters[8].Value = model.mobile;
            parameters[9].Value = model.transportcompany;
            parameters[10].Value = model.nuclearweight;
            parameters[11].Value = model.initialdate;
            parameters[12].Value = model.businessattribution;
            parameters[13].Value = model.businessdepartment;
            parameters[14].Value = model.businessowner;
            parameters[15].Value = model.option1;
            parameters[16].Value = model.option2;
            parameters[17].Value = model.option3;
            parameters[18].Value = model.inputtime;
            parameters[19].Value = model.isdel;
            parameters[20].Value = model.deltime;
            parameters[21].Value = model.inputor;
            parameters[22].Value = model.updatetime;
            parameters[23].Value = model.updateor;
            parameters[24].Value = model.state;
            parameters[25].Value = model.activityState;

            int rows = sqldal.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(FZKJ.Model.buss_vehicle model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update buss_vehicle set ");
            strSql.Append("vehiclecode=@vehiclecode,");
            strSql.Append("carowner=@carowner,");
            strSql.Append("carmodel=@carmodel,");
            strSql.Append("licenseplate=@licenseplate,");
            strSql.Append("drivingnumber=@drivingnumber,");
            strSql.Append("enginenumber=@enginenumber,");
            strSql.Append("pedestal=@pedestal,");
            strSql.Append("mobile=@mobile,");
            strSql.Append("transportcompany=@transportcompany,");
            strSql.Append("nuclearweight=@nuclearweight,");
            strSql.Append("initialdate=@initialdate,");
            strSql.Append("businessattribution=@businessattribution,");
            strSql.Append("businessdepartment=@businessdepartment,");
            strSql.Append("businessowner=@businessowner,");
            strSql.Append("option1=@option1,");
            strSql.Append("option2=@option2,");
            strSql.Append("option3=@option3,");
            strSql.Append("inputtime=@inputtime,");
            strSql.Append("isdel=@isdel,");
            strSql.Append("deltime=@deltime,");
            strSql.Append("inputor=@inputor,");
            strSql.Append("updatetime=@updatetime,");
            strSql.Append("updateor=@updateor,");
            strSql.Append("state=@state,");
            strSql.Append("activityState=@activityState");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@vehiclecode", SqlDbType.VarChar,30),
                    new SqlParameter("@carowner", SqlDbType.VarChar,36),
                    new SqlParameter("@carmodel", SqlDbType.VarChar,100),
                    new SqlParameter("@licenseplate", SqlDbType.VarChar,100),
                    new SqlParameter("@drivingnumber", SqlDbType.VarChar,100),
                    new SqlParameter("@enginenumber", SqlDbType.VarChar,100),
                    new SqlParameter("@pedestal", SqlDbType.Int,4),
                    new SqlParameter("@mobile", SqlDbType.VarChar,11),
                    new SqlParameter("@transportcompany", SqlDbType.VarChar,200),
                    new SqlParameter("@nuclearweight", SqlDbType.NVarChar,50),
                    new SqlParameter("@initialdate", SqlDbType.DateTime),
                    new SqlParameter("@businessattribution", SqlDbType.VarChar,10),
                    new SqlParameter("@businessdepartment", SqlDbType.VarChar,100),
                    new SqlParameter("@businessowner", SqlDbType.VarChar,100),
                    new SqlParameter("@option1", SqlDbType.VarChar,100),
                    new SqlParameter("@option2", SqlDbType.VarChar,100),
                    new SqlParameter("@option3", SqlDbType.VarChar,100),
                    new SqlParameter("@inputtime", SqlDbType.DateTime),
                    new SqlParameter("@isdel", SqlDbType.Int,4),
                    new SqlParameter("@deltime", SqlDbType.DateTime),
                    new SqlParameter("@inputor", SqlDbType.NVarChar,50),
                    new SqlParameter("@updatetime", SqlDbType.DateTime),
                    new SqlParameter("@updateor", SqlDbType.NVarChar,50),
                    new SqlParameter("@state", SqlDbType.Int,4),
                    new SqlParameter("@activityState", SqlDbType.Int,4),
                    new SqlParameter("@id", SqlDbType.VarChar,36)};
            parameters[0].Value = model.vehiclecode;
            parameters[1].Value = model.carowner;
            parameters[2].Value = model.carmodel;
            parameters[3].Value = model.licenseplate;
            parameters[4].Value = model.drivingnumber;
            parameters[5].Value = model.enginenumber;
            parameters[6].Value = model.pedestal;
            parameters[7].Value = model.mobile;
            parameters[8].Value = model.transportcompany;
            parameters[9].Value = model.nuclearweight;
            parameters[10].Value = model.initialdate;
            parameters[11].Value = model.businessattribution;
            parameters[12].Value = model.businessdepartment;
            parameters[13].Value = model.businessowner;
            parameters[14].Value = model.option1;
            parameters[15].Value = model.option2;
            parameters[16].Value = model.option3;
            parameters[17].Value = model.inputtime;
            parameters[18].Value = model.isdel;
            parameters[19].Value = model.deltime;
            parameters[20].Value = model.inputor;
            parameters[21].Value = model.updatetime;
            parameters[22].Value = model.updateor;
            parameters[23].Value = model.state;
            parameters[24].Value = model.activityState;
            parameters[25].Value = model.id;

            int rows = sqldal.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from buss_vehicle ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.VarChar,36)           };
            parameters[0].Value = id;

            int rows = sqldal.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from buss_vehicle ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = sqldal.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public FZKJ.Model.buss_vehicle GetModel(string id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,vehiclecode,carowner,carmodel,licenseplate,drivingnumber,enginenumber,pedestal,mobile,transportcompany,nuclearweight,initialdate,businessattribution,businessdepartment,businessowner,option1,option2,option3,inputtime,isdel,deltime,inputor,updatetime,updateor,state,activityState from buss_vehicle ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.VarChar,36)           };
            parameters[0].Value = id;

            FZKJ.Model.buss_vehicle model = new FZKJ.Model.buss_vehicle();
            DataSet ds = sqldal.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public FZKJ.Model.buss_vehicle DataRowToModel(DataRow row)
        {
            FZKJ.Model.buss_vehicle model = new FZKJ.Model.buss_vehicle();
            if (row != null)
            {
                if (row["id"] != null)
                {
                    model.id = row["id"].ToString();
                }
                if (row["vehiclecode"] != null)
                {
                    model.vehiclecode = row["vehiclecode"].ToString();
                }
                if (row["carowner"] != null)
                {
                    model.carowner = row["carowner"].ToString();
                }
                if (row["carmodel"] != null)
                {
                    model.carmodel = row["carmodel"].ToString();
                }
                if (row["licenseplate"] != null)
                {
                    model.licenseplate = row["licenseplate"].ToString();
                }
                if (row["drivingnumber"] != null)
                {
                    model.drivingnumber = row["drivingnumber"].ToString();
                }
                if (row["enginenumber"] != null)
                {
                    model.enginenumber = row["enginenumber"].ToString();
                }
                if (row["pedestal"] != null && row["pedestal"].ToString() != "")
                {
                    model.pedestal = int.Parse(row["pedestal"].ToString());
                }
                if (row["mobile"] != null)
                {
                    model.mobile = row["mobile"].ToString();
                }
                if (row["transportcompany"] != null)
                {
                    model.transportcompany = row["transportcompany"].ToString();
                }
                if (row["nuclearweight"] != null && row["nuclearweight"].ToString() != "")
                {
                    model.nuclearweight = row["nuclearweight"].ToString();
                }
                if (row["initialdate"] != null && row["initialdate"].ToString() != "")
                {
                    model.initialdate = DateTime.Parse(row["initialdate"].ToString());
                }
                if (row["businessattribution"] != null)
                {
                    model.businessattribution = row["businessattribution"].ToString();
                }
                if (row["businessdepartment"] != null)
                {
                    model.businessdepartment = row["businessdepartment"].ToString();
                }
                if (row["businessowner"] != null)
                {
                    model.businessowner = row["businessowner"].ToString();
                }
                if (row["option1"] != null)
                {
                    model.option1 = row["option1"].ToString();
                }
                if (row["option2"] != null)
                {
                    model.option2 = row["option2"].ToString();
                }
                if (row["option3"] != null)
                {
                    model.option3 = row["option3"].ToString();
                }
                if (row["inputtime"] != null && row["inputtime"].ToString() != "")
                {
                    model.inputtime = DateTime.Parse(row["inputtime"].ToString());
                }
                if (row["isdel"] != null && row["isdel"].ToString() != "")
                {
                    model.isdel = int.Parse(row["isdel"].ToString());
                }
                if (row["deltime"] != null && row["deltime"].ToString() != "")
                {
                    model.deltime = DateTime.Parse(row["deltime"].ToString());
                }
                if (row["inputor"] != null)
                {
                    model.inputor = row["inputor"].ToString();
                }
                if (row["updatetime"] != null && row["updatetime"].ToString() != "")
                {
                    model.updatetime = DateTime.Parse(row["updatetime"].ToString());
                }
                if (row["updateor"] != null)
                {
                    model.updateor = row["updateor"].ToString();
                }
                if (row["state"] != null && row["state"].ToString() != "")
                {
                    model.state = int.Parse(row["state"].ToString());
                }
                if (row["activityState"] != null)
                {
                    model.activityState = int.Parse(row["activityState"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,vehiclecode,carowner,carmodel,licenseplate,drivingnumber,enginenumber,pedestal,mobile,transportcompany,nuclearweight,initialdate,businessattribution,businessdepartment,businessowner,option1,option2,option3,inputtime,isdel,deltime,inputor,updatetime,updateor,state,activityState ");
            strSql.Append(" FROM buss_vehicle ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return sqldal.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,vehiclecode,carowner,carmodel,licenseplate,drivingnumber,enginenumber,pedestal,mobile,transportcompany,nuclearweight,initialdate,businessattribution,businessdepartment,businessowner,option1,option2,option3,inputtime,isdel,deltime,inputor,updatetime,updateor,state,activityState ");
            strSql.Append(" FROM buss_vehicle ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return sqldal.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM buss_vehicle ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = sqldal.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from buss_vehicle T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            if (endIndex > 0)
                strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return sqldal.Query(strSql.ToString());
        }

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "buss_vehicle";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return sqldal.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(string where, int state)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update buss_vehicle set ");
            strSql.Append("state=" + state);
            strSql.Append(" where id in (" + where + ")");
            int rows = sqldal.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion  ExtensionMethod
    }
}

